package com.example.ywj.toutiao;

import androidx.appcompat.app.AppCompatActivity;
import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.ywj.toutiao.bean.News;
import com.example.ywj.toutiao.utils.Constant;
import com.example.ywj.toutiao.utils.MySaveListBaseAdapter;
import com.example.ywj.toutiao.utils.ResponseApi;
import com.example.ywj.toutiao.utils.SpUtils;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

public class LikeListActivity extends AppCompatActivity {
    private static final String TAG = "LikeListActivity";
    @BindView(R.id.like_list_back)
    public ImageView like_list_back;
    @BindView(R.id.like_list_nodata)
    public LinearLayout like_list_nodata;
    @BindView(R.id.like_list_list1)
    public ListView like_list_list1;
    public List<News> list = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_like_list);
        ButterKnife.bind(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {//因为不是所有的系统都可以设置颜色的，在4.4以下就不可以。。有的说4.1，所以在设置的时候要检查一下系统版本是否是4.1以上
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(getResources().getColor(R.color.trgray));
        }
        String username = SpUtils.getString(LikeListActivity.this,"username");
        initView();
        findLikeList(username);
    }
    public void initView(){
        like_list_back.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public void findLikeList(String username){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Constant.BASEURL)
                .build();
        ResponseApi responseApi = retrofit.create(ResponseApi.class);
        Call<ResponseBody> call = responseApi.findLikeList(username);
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                String json = null;
                try{
                    json = response.body().string();
                }catch (Exception e){
                    e.printStackTrace();
                }
                Log.i(TAG, "onResponse: "+json);
                JsonObject obj = (JsonObject)new JsonParser().parse(json);
                Integer code = obj.get("statusCode").getAsInt();
                if(code==200&&obj.has("count")){
                    like_list_nodata.setVisibility(View.VISIBLE);
                    like_list_list1.setVisibility(View.GONE);
                }else if(code == 200&& obj.has("data")) {
                    like_list_nodata.setVisibility(View.GONE);
                    like_list_list1.setVisibility(View.VISIBLE);
                    JsonArray objArr = obj.getAsJsonArray("data");
                    Gson gson = new Gson();
                    list = gson.fromJson(objArr, new TypeToken<ArrayList<News>>() {}.getType());
                    MySaveListBaseAdapter mySaveListBaseAdapter = new MySaveListBaseAdapter(list);
                    like_list_list1.setAdapter(mySaveListBaseAdapter);
                    like_list_list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Bundle bundle = new Bundle();
                            bundle.putString("id",list.get(position).get_id());
                            bundle.putString("otherUser",list.get(position).getUsername());
                            Intent intent = new Intent(parent.getContext(),DetailActivity.class);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.i(TAG, "onFailure: "+t);
            }
        });
    }
}
